rm(list=ls(all=TRUE))
library(metafor)
library(clubSandwich)
library(dplyr)

######################################
##### LIT
######################################
setwd("~/Dropbox/Apps/ShareLaTeX/Minimal Effects/replication/data")

data <- read.csv("MPSA Persuasion MetaAnalysis - Candidate Campaign.csv", stringsAsFactors = FALSE)

# Recoding fun
data <- rename(data, CACE = CACE..in.pp., SE = SE..in.pp.)
data$CACE <- data$CACE * 100
data$SE <- data$SE * 100

data <- subset(data, Primary.or.General == 'General')

data$Experiment <- factor(data$Experiment...for.clustering)

data$Personal <- ifelse(data$Treatment.Mode %in% c("Candidate Canvass", "Canvass",
                                                   "Phone"), 1, 0)

data$Treatment.days.before.election <- with(data, Days.after.treatment.survey.was.taken - Days.after.election.survey.was.taken)
data$Treatment.Within.60.Days <- ifelse(data$Treatment.days.before.election < 60, 1, 0)

# Remove early studies that measure early
data <- subset(data, !(data$Treatment.days.before.election >= 60 &
                         data$Days.after.treatment.survey.was.taken <= 8))

lit <- select(data, CACE, SE,
              Measurement.Mode = Measurement.mode,
              Experiment.for.Clustering = Experiment,
              Personal, Treatment.Mode, Treatment.Within.60.Days)
lit$label <- as.character(lit$Experiment.for.Clustering)
lit$label <- paste0(lit$label, ' - ', lit$Treatment.Mode)
lit$WA <- 0

###################################
########### WA
###################################
data <- read.csv("master_sheet_output.csv", stringsAsFactors = FALSE)
data <- subset(data, !is.na(Candidate.Effect.with.Covars))

data$ExperimentName <- gsub("Canvass", "Experiment", data$ExperimentName)
data$label <- paste0(data$ExperimentName, ' - ', data$Seat, ' - ', data$Treatment.Mode)

# Remove non-partisan and non-general races
data <- subset(data, !(data$label %in% c("Original Study - NC DID - Supreme Court - Warm Lit Drop",
                                         "Original Study - NC Experiment - Supreme Court - Warm Lit Drop",
                                         "Original Study - PA Experiment 2015 - Mayor - Canvass",
                                         "Original Study - PA Experiment 2015 Follow-Up - Mayor - Canvass",
                                         "Original Study - WA Experiment 2015 Post-Election - State Legislator - Canvass",
                                         "Original Study - WA Experiment 2015 - State Legislator - Canvass")))

data$Treatment.Within.60.Days <- as.numeric(data$Data != 'OH_EarlyCanvass_2016.dta' & # Canvass in May
                                            data$Data != 'OH_LateCanvass_2016.dta') # Canvass in August

data <- data[order(data$Date.Canvass),]
# Convert to pp and calcualte variance
data$CACE <- data$Candidate.Effect.with.Covars * 50
data$SE <- data$Candidate.Effect.SE.with.Covars * 50
#data$label <- paste0('Original Study - ', toupper(substr(data$Experiment, 1 ,2)), ' - ', data$Seat)
#data$ExperimentName <- gsub("Canvass", "Experiment", data$ExperimentName)
#data$label <- paste0(data$ExperimentName, ' - ', data$Seat)

data <- subset(data, Primary.or.General == 'General')

wa <- select(data, CACE, SE, Experiment.for.Clustering = Experiment, label,
             Treatment.Within.60.Days, Treatment.Mode)
wa$Measurement.Mode <- 'Survey'
wa$Personal <- 1
wa$WA <- 1

# Merge
data <- rbind.data.frame(lit, wa)

data$variance <- data$SE^2
data$precision <- 1/data$variance

#Save a merged data set
write.csv(data, "merged_data.csv")

## OVERALL EFFECTS
# How much precision is there?
wa.vs.lit.precision.and.effects <- function(subset, x.title = "") {
  sub <- data[subset,]
  
  # Precision totals
  ptotals <- by(sub$precision, sub$WA, sum)
  
  # Meta-analysis  
  meta <- rma.mv(yi = CACE, V = variance, random = list(~1 | Experiment.for.Clustering),
                 data = sub)
  
  forest(meta, slab = paste0(sub$label), cex = .8,
         alim=c(-40,40), xlim=c(-85,65),
         xlab = x.title)
  
  return(list(ptotals = ptotals,
              meta = coef_test(meta, vcov = "CR2")))
}

wa.vs.lit.precision.and.effects(data$Treatment.Within.60.Days == 1)
wa.vs.lit.precision.and.effects(data$Personal == 1 & data$Treatment.Within.60.Days == 1)
wa.vs.lit.precision.and.effects(data$Personal == 1)
wa.vs.lit.precision.and.effects(data$Personal == 0)

wa.vs.lit.precision.and.effects(data$WA == 1 & data$Treatment.Within.60.Days == 1)

#Effect of WA vs. Non-WA
non.wa <- rma.mv(yi = CACE, V = variance, random = list(~1 | Experiment.for.Clustering),
       data = subset(data, data$WA == 0 & data$Personal == 1 & data$Treatment.Within.60.Days == 1))
coef_test(non.wa, vcov = "CR2")
wa <- rma.mv(yi = CACE, V = variance, random = list(~1 | Experiment.for.Clustering),
       data = subset(data, data$WA == 1 & data$Personal == 1 & data$Treatment.Within.60.Days == 1))
coef_test(wa, vcov = "CR2")
combined <- rma.mv(yi = CACE, V = variance, random = list(~1 | Experiment.for.Clustering),
       data = subset(data, data$Personal == 1 & data$Treatment.Within.60.Days == 1))
coef_test(combined, vcov = "CR2")

pdf('../figures/withwa_impersonal_contact.pdf', width = 10, height = 5)
wa.vs.lit.precision.and.effects(data$Personal == 0 & data$Treatment.Within.60.Days == 1, 
                                x.title = 'Estimated Treatment Effect (CACE) in Percentage Points and 95% Confidence Interval \n Subset: Impersonal Contact - Treatment Within 2 Months of Election Day')
dev.off()

pdf('../figures/withwa_personal_contact.pdf', width = 10, height = 6)
wa.vs.lit.precision.and.effects(data$Personal == 1 & data$Treatment.Within.60.Days == 1,
                                x.title = 'Estimated Treatment Effect (CACE) in Percentage Points and 95% Confidence Interval \n Subset: Personal Contact - Treatment Within 2 Months of Election Day')
dev.off()

# Compare WA studies to academic lit; remove private study we included in meta-analysis
data <- subset(data, substr(label, 1, 5) != 'Cunow')

# WA studies increase the precision of the personal literature done close to election by factor of ~5
sum(data$precision[data$WA == 1 & data$Personal == 1]) /
  sum(data$precision[data$WA == 0 & data$Personal == 1])

# WA studies increase the precision of the personal literature done close to election by factor of ~7
sum(data$precision[data$Treatment.Within.60.Days == 1 & data$WA == 1 & data$Personal == 1]) /
    sum(data$precision[data$Treatment.Within.60.Days == 1 & data$WA == 0 & data$Personal == 1])

